package com.micro.glszh.stat.controller;

import com.micro.glszh.stat.base.CommonResult;
import com.micro.glszh.stat.domain.BizBaseRecord;
import com.micro.glszh.stat.domain.dto.RecordDto;
import com.micro.glszh.stat.interceptor.AuthCheck;
import com.micro.glszh.stat.service.BizBaseRecordService;
import com.micro.glszh.stat.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * BizBaseRecordController
 *
 * @Author gaopengfei
 * @create 2024/10/21 16:12
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "基础记录")
@RequestMapping("/api/bizBaseRecord")
public class BizBaseRecordController {

    private final BizBaseRecordService baseRecordService;

    @AuthCheck
    @ApiOperation("分页按日期获取统计列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页，默认第一页", defaultValue = "1", required = false),
            @ApiImplicitParam(name = "size", value = "每页多少条，默认10条", required = false)
    })
    @GetMapping("/list")
    public CommonResult<PageResult<Map<String, Object>>> listUser(String dateTime, Integer page, Integer size) {
        return CommonResult.success(baseRecordService.list(dateTime, page, size));
    }

    @AuthCheck
    @ApiOperation("查询当日日记录")
    @GetMapping()
    public CommonResult<List<BizBaseRecord>> selectRecordByDateTime(String dateTime) {
        return CommonResult.success(baseRecordService.selectRecordByDateTime(dateTime));
    }

    @AuthCheck
    @ApiOperation("查询个人登记最新的记录")
    @GetMapping("/latest")
    public CommonResult<BizBaseRecord> selectRecordLatest() {
        return CommonResult.success(baseRecordService.selectRecordLatest());
    }

    @AuthCheck
    @ApiOperation("下载当日日记录")
    @GetMapping("/download")
    public CommonResult<Void> downloadRecordByDateTime(String dateTime, HttpServletResponse response) {
        baseRecordService.downloadRecordByDateTime(dateTime, response);
        return CommonResult.success();
    }

    @AuthCheck
    @ApiOperation("新增一次记录")
    @PostMapping()
    public CommonResult<Boolean> insertOrUpdateRecord(@Validated @RequestBody RecordDto record) {
        return CommonResult.success(baseRecordService.insertOrUpdateRecord(record));
    }

    @AuthCheck
    @ApiOperation("删除一条记录")
    @DeleteMapping("/{id}")
    public CommonResult<Boolean> deleteDiseaseDetail(@PathVariable Long id) {
        return CommonResult.success(baseRecordService.removeById(id));
    }
}
